package com.itcast.service.impl;

import com.itcast.dao.ShoppingDao;
import com.itcast.dao.Shopping_OrderDao;
import com.itcast.domain.Shopping;
import com.itcast.domain.Shopping_Order;
import com.itcast.service.OrderService;
import com.itcast.utils.ResponseBodyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service("orderService")
public class OrderServiceImpl implements OrderService {

    @Autowired
    private ShoppingDao shoppingDao;

    @Autowired
    private Shopping_OrderDao orderDao;


    /**
     * 结算购物车，形成订单
     * @param shopping
     * @return
     * @throws Exception
     */
    @Override
    public String settle(List<Shopping> shopping) throws Exception {
        List<Shopping_Order> list = new ArrayList<>();
        if (shopping.size() != 0) {
            for (Shopping shop : shopping) {
                Shopping_Order order = new Shopping_Order();
                Double totalPrice = 0.0;
                order.setName(shop.getName());
                order.setNumber(shop.getNumber());
                totalPrice += shop.getPrice() * shop.getNumber();
                order.setPrice(totalPrice);
                order.setTime(new Date());
                list.add(order);
            }
            int count = orderDao.settle(list);
            if (count > 0) {
                shoppingDao.deleteAll();
                return ResponseBodyUtil.getSuccessNoObject("创建");
            } else {
                return ResponseBodyUtil.getUnSuccessNoObject("创建");
            }
        } else {
            return ResponseBodyUtil.getUnSuccessNoObject("创建");
        }

    }

    /**
     * 查询所有订单
     * @return
     */
    @Override
    public List<Shopping_Order> findAll() {
        List<Shopping_Order> all = orderDao.findAll();
        return all;
    }

    /**
     * 删除单个订单
     * @return
     * @throws Exception
     * @param time
     */
    @Override
    public String byTimeDelete(String time) throws Exception {
        if (time != null) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = formatter.parse(time);
            int count = orderDao.byTimeDelete(date);
            if (count > 0) {
                return ResponseBodyUtil.getSuccessNoObject("删除");
            } else {
                return ResponseBodyUtil.getUnSuccessNoObject("删除");
            }
        } else {
            return ResponseBodyUtil.getUnSuccessNoObject("删除");
        }
    }

    /**
     * 查询所有订单时间
     * @return
     */
    @Override
    public TreeSet<Date> findTime() {
        return orderDao.findTime();
    }

    /**
     * 通过订单时间查询某个时间下的订单
     * @return
     * @param s
     */
    @Override
    public List<Shopping_Order> byTimeFind(String s) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = formatter.parse(s);
        return orderDao.byTimeFind(date);
    }
}
